package com.omt.tarjimdinek.web.member;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.util.StringUtils;

import com.omt.tarjimdinek.domain.tv.entity.Cheikh;
import com.omt.tarjimdinek.domain.tv.service.CheikhRepository;
import com.omt.tarjimdinek.util.Constant;
import com.omt.tarjimdinek.util.StringHelper;
import com.omt.tarjimdinek.util.UserContextUtil;

/**
 * submit a request
 * @author chabane
 *
 */
@Named("submittingRequest")
@Scope("session")
public class SubmittingRequestController {

	private ResourceBundle message = ResourceBundle.getBundle("localization.messages_member", UserContextUtil.getLocaleInstance());
	private SubmittingRequestCriteria criteria = new SubmittingRequestCriteria();
    private static final Logger log = Logger.getLogger(SubmittingRequestController.class);
    
	@Inject
	private CheikhRepository cheikhRepository;
	
	private List<Cheikh> listeCheikh;
	
    /**
     * Check if youtube url is correct and convert url to valid link
     * @return
     */
    public boolean checkYoutubeLink(){
    	boolean lIsValid = StringHelper.validateAndConvertYoutubeURL(criteria.getYoutubeLink());
    	if(StringUtils.hasLength(criteria.getYoutubeLink())) {
    		criteria.setYoutubeLinkShowing(criteria.getYoutubeLink().replace("watch?v=", "v/"));
    	}
    	return lIsValid;
    }
    
    /**
     * Go to submit a request page
     * @return submit request page
     */
    public String goSubmitRequest() {
    	
    	criteria = new SubmittingRequestCriteria();
    	criteria.setListContributors(new ArrayList<String>());
    	if(listeCheikh == null){
    		listeCheikh = cheikhRepository.findAll();
    	}
    	criteria.setListOriginalTextVideo(new ArrayList<OriginalTextVideo>());
    	criteria.getListOriginalTextVideo().add(new OriginalTextVideo());
    	return RequestUtil.SUBMIT_REQUEST_PAGE + Constant.REDIRECT;
    }

    /**
     * Used by autocomplete component to show all cheikh from database
     * @return List<Cheikh>
     */
    public List<String> completeIntervenants(String lValue){
    	List<String> lSuggestions = new ArrayList<String>();
    	
        for(Cheikh lCheikh : listeCheikh) {  
            if(lCheikh.getNom().toUpperCase().startsWith(lValue.toUpperCase())) { 
            	lSuggestions.add(lCheikh.getNom());  
            }
        }  
        
        if(lSuggestions.isEmpty()) {
        	lSuggestions.add(lValue);
        }
          
        return lSuggestions;
    }
    
	/**
	 * @return the criteria
	 */
	public SubmittingRequestCriteria getCriteria() {
		return criteria;
	}

	/**
	 * @param criteria the criteria to set
	 */
	public void setCriteria(SubmittingRequestCriteria criteria) {
		this.criteria = criteria;
	}

	/**
	 * @return the cheikhRepository
	 */
	public CheikhRepository getCheikhRepository() {
		return cheikhRepository;
	}

	/**
	 * @param cheikhRepository the cheikhRepository to set
	 */
	public void setCheikhRepository(CheikhRepository cheikhRepository) {
		this.cheikhRepository = cheikhRepository;
	}
}
